﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using canthovieclam.BL.Models;
using canthovieclam.DAL.Repositories;

namespace canthovieclam.BL
{
    public class PostJobImpl
    {
        public bool InsertToDatabase(JobModel jobModel, int locationId, int levelId, Guid jobCategoryId)
        {
            IUnitOfWork unitOfWork = new UnitOfWork();

            var jobRepository = new JobRepository(unitOfWork);
            var job = jobModel.MapJob();
            jobRepository.AddJob(job);

            var locationRepository = new LocationRepository(unitOfWork);
            var location = locationRepository.GetSingleLocation(x => x.LocationId == locationId);
            job.Location.Add(location);

            var levelRepository = new LevelRepository(unitOfWork);
            var level = levelRepository.GetSingleLevel(x => x.LevelId == levelId);
            job.Level.Add(level);

            var jobCategoryRepository = new JobCategoryRepository(unitOfWork);
            var jobCategory = jobCategoryRepository.GetSingleJobCategory(x => x.JobCategoryId == jobCategoryId);
            job.JobCategory.Add(jobCategory);


            unitOfWork.Commit();
            return true;
        }

        public Guid GetEmployerIdFromUserId(Guid userId)
        {
            var employerRepository = new EmployerRepository();
            var contactRepository = new ContactRepository();
            var employerContactRepository = new EmployerContactRepository();
            var employerId = (from x in contactRepository.GetAllContact()
                              from b in x.EmployerContact
                              where x.LoginId == userId
                              select b.EmployerId).FirstOrDefault();
            return employerId;
        }             
    }
}
